package dearchenpush.good.com.qianfanlive.modules.discover;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.recker.flybanner.FlyBanner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.discover.adapter.ViewPagerFragmentAdapter;
import dearchenpush.good.com.qianfanlive.modules.discover.adapter.ViewPagerHeadAdapter;
import dearchenpush.good.com.qianfanlive.modules.discover.bean.ViewPagerBean;
import dearchenpush.good.com.qianfanlive.modules.discover.fragment.DiscoverHotFragment;
import dearchenpush.good.com.qianfanlive.modules.discover.fragment.DiscoverLikeFragment;
import dearchenpush.good.com.qianfanlive.ui.ShopActivity;
import dearchenpush.good.com.qianfanlive.url.UrlConfigs;
import dearchenpush.good.com.qianfanlive.utils.CircleIndicator;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment {


    @BindView(R.id.textView_disFragment_work)
    TextView mWork;
    @BindView(R.id.relativeLayout_discover_search)
    RelativeLayout mSearch;
    @BindView(R.id.text_disFragment_shop)
    TextView mShop;
//    @BindView(R.id.viewPage_disFragment_show)
   public FlyBanner mViewPage;
    @BindView(R.id.tabLayout_disFragment_1)
    TabLayout mTabLayout1;
    @BindView(R.id.tabLayout_disFragment_2)
    TabLayout mTabLayout2;
    @BindView(R.id.viewPager_disFragment_content)
    ViewPager mViewPagerContent;
    private CircleIndicator mIndicator;

    private ObservableScrollView mScrollView;


    //上边viewPager广告的数据
    private List<ViewPagerBean.MessageBean.BannersBean> mViewPagerData = new ArrayList<>();
    private ViewPagerHeadAdapter mViewPagerAdapter;

    //下边viewPager里的Fragment
    private List<Fragment> mFragments;
    private ViewPagerFragmentAdapter mFragmentAdapter;
//    public ImageHandler mImageHandler = new ImageHandler(new WeakReference<DiscoverFragment>(this));

    public DiscoverFragment() {
        // Required empty public constructor
    }

    public static DiscoverFragment newInstance() {
        DiscoverFragment fragment = new DiscoverFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        mViewPage = (FlyBanner) view.findViewById(R.id.viewPage_disFragment_show);
        mScrollView = (ObservableScrollView) view.findViewById(R.id.scrollView_disFragment);

//        mIndicator = (CircleIndicator) view.findViewById(R.id.indicator_disFragment);
//        mIndicator.setIndicatorMode(CircleIndicator.Mode.OUTSIDE);
//        mIndicator.setIndicatorMargin(25);
//        mIndicator.setIndicatorRadius(6);


        ButterKnife.bind(this, view);

        mTabLayout1.setVisibility(View.GONE);
        initFragment();
        initData();
        setData();

        mScrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                Log.d("aaaa", "onScrollChanged: "+y);
                if (y > 565) {
                    mTabLayout2.setVisibility(View.INVISIBLE);
                    mTabLayout1.setVisibility(View.VISIBLE);
                } else {
                    mTabLayout2.setVisibility(View.VISIBLE);
                    mTabLayout1.setVisibility(View.GONE);
                }
//
            }


        });

//        mViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                mImageHandler.sendMessage(Message.obtain(mImageHandler, ImageHandler.IMAGE_PAGE, position, 0));
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//                switch (state) {
//                    case ViewPager.SCROLL_STATE_DRAGGING://正在滑动
//                        mImageHandler.sendEmptyMessage(ImageHandler.IMAGE_PAUSE);
//                        break;
//                }
//            }
//        });
//        mViewPage.setCurrentItem(1000/2);
//        mImageHandler.sendEmptyMessageDelayed(ImageHandler.IMAGE_UPDATE, ImageHandler.IMAGE_DURATION);

        return view;
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(DiscoverLikeFragment.newInstance());
        mFragments.add(DiscoverHotFragment.newInstance());
    }

    private void setData() {
        //顶部广告Viewpager
//        mViewPage.setAdapter(mViewPagerAdapter);
        loadViewPagerData();

        //下边ViewPager
        mViewPagerContent.setAdapter(mFragmentAdapter);
        //tablayout2跟viewpager联动
        mTabLayout2.setupWithViewPager(mViewPagerContent);
        mTabLayout1.setupWithViewPager(mViewPagerContent);
    }

    private void initData() {
//        mViewPagerAdapter = new ViewPagerHeadAdapter(mViewPagerData, getContext());
        mFragmentAdapter = new ViewPagerFragmentAdapter(getChildFragmentManager(), mFragments, getContext());
    }

    //下载顶部广告的数据
    public void loadViewPagerData() {
        OkHttpUtils.get().url(UrlConfigs.DISCOVER_TOP_CAROUSEL)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                ViewPagerBean bean = gson.fromJson(response, ViewPagerBean.class);
                mViewPagerData.addAll(bean.getMessage().getBanners());
                List<String> url = new ArrayList<String>();
                for (int i = 0; i < mViewPagerData.size(); i++) {
                    url.add(mViewPagerData.get(i).getPicUrl());
                }
                mViewPage.setImagesUrl(url);


//                if (mViewPagerData.size() > 0) {
//                    mIndicator.setViewPager(mViewPage,mViewPagerData.size());
//                }
                //数据下载完成 刷新适配器
//                mViewPagerAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.textView_disFragment_work, R.id.relativeLayout_discover_search, R.id.text_disFragment_shop})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textView_disFragment_work:
                break;
            case R.id.relativeLayout_discover_search:
                break;
            case R.id.text_disFragment_shop:
                //点击商城 跳转到shop页面
                startActivity(new Intent(getContext(), ShopActivity.class));
                break;
        }
    }




}
